<template>
  <div class="xtx-goods-page">
    <div class="container" v-if="goods.categories">
      <!-- 面包屑 -->
      <XtxBread>
        <XtxBreadItem to="/">首页</XtxBreadItem>
        <XtxBreadItem :to="`/category/${goods.categories[1].id}`">
          {{ goods.categories[1].name }}
        </XtxBreadItem>
        <XtxBreadItem :to="`/category/sub/${goods.categories[0].id}`">
          {{ goods.categories[0].name }}
        </XtxBreadItem>
        <XtxBreadItem>
          {{ goods.name }}
        </XtxBreadItem>
      </XtxBread>
      <!-- 商品信息 -->
      <!-- 商品信息 -->
      <div class="goods-info">
        <!-- 图片预览区 -->
        <div class="media">
          <!-- 图片预览区 -->
          <XtxImageView :image-list="goods.mainPictures" />
          <!-- 统计数量 -->
          <GoodsSale :goods="goods"></GoodsSale>
        </div>
        <!-- 商品信息区 -->
        <div class="spec">
          <!-- 商品信息区 -->
          <GoodsInfo :goods="goods"></GoodsInfo>
          <!-- Sku组件 -->
          <!-- <GoodsSku
            :goods="goods"
            @change="handleChange"
            skuId="1369155867420659714"
          ></GoodsSku> -->
          <GoodsSku :goods="goods" @change="handleChange"></GoodsSku>
          <!-- 数量选择子组件 -->
          <XtxNumbox v-model="count" :max="maxCount" />
          <!-- 按钮组件 -->
          <XtxButton type="primary" style="margin-top: 20px" @click="addToCart">
            加入购物车
          </XtxButton>
        </div>
      </div>
      <!-- 商品详情 -->
      <div class="goods-footer">
        <!-- 商品详情 -->
        <GoodsDetail :goods="goods" />
        <!-- 24热榜+专题推荐 -->
        <div class="goods-aside">
          <GoodsHot :type="1" />
          <GoodsHot :type="2" />
          <GoodsHot :type="3" />
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { reqFindGoods } from '@/api/goods'
import { ref, computed } from 'vue'
import { useRoute } from 'vue-router'
import { useStore } from 'vuex'
import GoodsSale from './components/goods-sale'
import GoodsInfo from './components/goods-info'
import GoodsSku from './components/goods-sku'
import GoodsDetail from './components/goods-detail'
import GoodsHot from './components/goods-hot'
import { message } from '@/components/Message/index.js'

export default {
  name: 'XtxGoodsPage',
  components: {
    GoodsSale,
    GoodsInfo,
    GoodsSku,
    GoodsDetail,
    GoodsHot
  },
  setup () {
    const route = useRoute()
    const store = useStore()

    const goods = ref({})
    async function getGoods () {
      const res = await reqFindGoods(route.params.id)
      goods.value = res.result

      console.log(res.result)
    }
    getGoods()

    const currSku = ref({})
    const handleChange = sku => {
      if (sku.skuId) {
        goods.value.price = sku.price
        goods.value.oldPrice = sku.oldPrice
        goods.value.inventory = sku.inventory

        currSku.value = sku
      }
    }

    const count = ref(1)
    const maxCount = computed(() => {
      return goods.value.inventory > 0 ? goods.value.inventory : 1
    })

    // 加入购物车
    const addToCart = async () => {
      // 判断是否把规格选全了
      if (!currSku.value.skuId) {
        message({ type: 'warn', text: '请选择完整的规格!' })
        return
      }

      // 判断选中的数量是否大于库存
      if (count.value > currSku.value.inventory) {
        message({ type: 'warn', text: '您购买的商品超过了最大库存量!' })
        return
      }

      await store.dispatch('cart/asyncAddToCart', {
        id: goods.value.id,
        name: goods.value.name,
        picture: goods.value.mainPictures[0],
        skuId: currSku.value.skuId,
        price: currSku.value.oldPrice,
        nowPrice: currSku.value.price,
        // nowPrice: 1,
        attrsText: currSku.value.specsText,
        stock: currSku.value.inventory,
        selected: true, // 选中
        isEffective: true, // 是否有效
        // isEffective: false, // 是否有效
        count: count.value // 商品数量
      })

      message({ type: 'success', text: '加入购物车成功~' })
    }

    return {
      goods,
      handleChange,
      count,
      maxCount,
      addToCart
    }
  }
}
</script>

<style scoped lang="less">
.goods-info {
  min-height: 600px;
  background: #fff;
  display: flex;
  .media {
    width: 580px;
    height: 600px;
    padding: 30px 50px;
  }
  .spec {
    flex: 1;
    padding: 30px 30px 30px 0;
  }
}
.goods-footer {
  display: flex;
  margin-top: 20px;
  .goods-article {
    width: 940px;
    margin-right: 20px;
  }
  .goods-aside {
    width: 280px;
    min-height: 1000px;
  }
}
.goods-tabs {
  min-height: 600px;
  background: #fff;
}
.goods-warn {
  min-height: 600px;
  background: #fff;
  margin-top: 20px;
}
</style>
